package me.xue.communityhealth.admin.controller;

import me.xue.communityhealth.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class PostController {

    private final PostService postService;

    @Autowired
    public PostController(PostService postService) {
        this.postService = postService;
    }

    @GetMapping("/posts")
    public String departments(Model model) {
        List<String> posts = postService.findAll();
        model.addAttribute("posts", posts);
        return "posts";
    }

    @PostMapping("/post")
    @ResponseBody
    public String addDepartment(@RequestParam("post") String post) {
        if (StringUtils.isEmpty(post)) {
            return "职务不能为空";
        }
        int row = postService.save(post);
        if (row == 1) {
            return "success";
        }
        return "添加失败";
    }

    @DeleteMapping("/post/{postName}")
    @ResponseBody
    public String deleteDepartment(@PathVariable("postName") String post) {
        if (StringUtils.isEmpty(post)) {
            return "删除失败";
        }
        int row = postService.delete(post);
        if (row == 1) {
            return "success";
        }
        return "删除失败";
    }

}
